package com.atguigu.gmall.user.controller;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.user.entity.UserInfo;
import com.atguigu.gmall.user.service.UserInfoService;
import com.atguigu.gmall.user.vo.LoginRespVo;
import io.swagger.annotations.ResponseHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-15 11:43
 **/
@RequestMapping("/api/user")
@RestController
public class UserApiController {
    @Autowired
    UserInfoService userInfoService;

    /**
     * 登录功能
     *
     * @param userInfo
     * @param request
     * @return
     */
    @PostMapping("/passport/login")
    public Result login(@RequestBody UserInfo userInfo, HttpServletRequest request) {
        LoginRespVo respVo = userInfoService.login(userInfo, request);
        return Result.ok(respVo);
    }

    /**
     * 退出功能
     *
     * @param token
     * @return
     */
    @GetMapping("/passport/logout")
    public Result logout(@RequestHeader(value = "token", required = false) String token) {
        if (!StringUtils.isEmpty(token)) {
            userInfoService.logout(token);
        }
        return Result.ok();
    }
}
